package com.ccl.iot.updater;import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ccl.iot.R;
import com.ccl.iot.publics.StringFactory;

public class DownloadDialog extends AlertDialog.Builder {
	private AlertDialog iDialog = null;
	
	private LinearLayout iLayout = null;
	private ImageView iIconView = null;
	
	private LinearLayout iRightFrame = null;
	private TextView iTitleView = null;
	
	private ProgressBar iProgressBarView = null;
	
	private RelativeLayout iProgressFrame = null;
	private RelativeLayout.LayoutParams iProgressLayoutParams = null;
	
	private TextView iProgressView = null;
	private TextView iProgressPercentView = null;
	
	private int iIcon = 0;
	private String iTitle = null;
	private int iTotalSize = 0;
	private boolean iCancelable = false;
	private boolean iBackgroundable = false;
	
	private long iLastProgressTime = 0;
	
	public DownloadDialog(Context aContext, int aIcon, String aTitle, int aTotalSize, boolean aCancelable, boolean aBackgroundable) {
		super(aContext, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);

		iIcon = aIcon;
		iTitle = aTitle;
		iTotalSize = aTotalSize;
		iCancelable = aCancelable;
		iBackgroundable = aBackgroundable;
		
		if (iCancelable == false && iBackgroundable == false){
			this.setCancelable(false);
		}
		
		initializeView();
	}

	private void initializeView(){
		if (iLayout == null){
			iLayout = new LinearLayout(this.getContext());
			iLayout.setOrientation(LinearLayout.HORIZONTAL);
			iLayout.setPadding(10, 10, 10, 10);
			
			iIconView = new ImageView(this.getContext());
			iIconView.setImageResource(iIcon);
			iLayout.addView(iIconView);
			
			iRightFrame = new LinearLayout(this.getContext());
			iRightFrame.setOrientation(LinearLayout.VERTICAL);
			iLayout.addView(iRightFrame);
			
			iTitleView = new TextView(this.getContext());
			iTitleView.setText(iTitle);
			iRightFrame.addView(iTitleView);
			
			iProgressBarView = new ProgressBar(this.getContext(), null, android.R.attr.progressBarStyleHorizontal);
			iProgressBarView.setMax(iTotalSize);
			iRightFrame.addView(iProgressBarView);
						
			iProgressFrame = new RelativeLayout(this.getContext());
			iRightFrame.addView(iProgressFrame, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
			
			iProgressView = new TextView(this.getContext());
			iProgressView.setText("0/" + iTotalSize);
			iProgressLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
			iProgressLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
			iProgressFrame.addView(iProgressView, iProgressLayoutParams);
			
			iProgressPercentView = new TextView(this.getContext());
			iProgressPercentView.setText("0%");
			iProgressLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
			iProgressLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
			iProgressFrame.addView(iProgressPercentView, iProgressLayoutParams);
			
			if (iCancelable){
				this.setNegativeButton(R.string.Cancel, new OnClickListener(){
					@Override
					public void onClick(DialogInterface dialog, int which) {
					}				
				});
			}
			
			if (iBackgroundable){
				this.setPositiveButton(R.string.Background, new OnClickListener(){
					@Override
					public void onClick(DialogInterface dialog, int which) {
					}			
				});
			}

			this.setView(iLayout);
		}
	}
	
	@Override
	public AlertDialog show() {
		iDialog = super.show();
		
		return iDialog;
	}
	
	public void dismiss(){
		if (iDialog != null){
			iDialog.dismiss();
			
			iDialog = null;
		}
	}

	public void setProgress(int aProgress){
		if (iProgressBarView != null){
			if (aProgress != iProgressBarView.getProgress()){
				long tTime = System.currentTimeMillis();
				long tTimeUsed = tTime - iLastProgressTime;
				
				if (tTimeUsed > 200){
					if (iProgressView != null){
						iProgressView.setText(StringFactory.GetSizeDescription(aProgress, 0) + "(" + StringFactory.GetSizeDescription((aProgress - iProgressBarView.getProgress()) / tTimeUsed * 1000, 0) + "/s)");
					}
					
					iProgressBarView.setProgress(aProgress);
					
					if (iProgressPercentView != null){
						iProgressPercentView.setText((int) ((double)aProgress / (double)iProgressBarView.getMax() * 100) + "%");
					}
					
					iLastProgressTime = System.currentTimeMillis();
				}
			}
		}
	}
	
	public int getTotalSize(){
		return iTotalSize;
	}
	
	public int getProgress(){
		if (iProgressBarView != null){
			return iProgressBarView.getProgress();
		}
		
		return 0;
	}
}
